PairSet

Ecng.Collections

Represents a collection of key-value pairs that supports bidirectional lookup by key or value.

Inherits: KeyedCollection<T, T>

Constructors

PairSet
public PairSet(IEqualityComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
pairSet = PairSet(keyComparer, valueComparer)

Initializes a new instance of the PairSet class with specified key and value comparers.

keyComparer
The equality comparer for keys, or null to use the default comparer.
valueComparer
The equality comparer for values, or null to use the default comparer.
PairSet
public PairSet(IEqualityComparer<TKey> comparer)
pairSet = PairSet(comparer)

Initializes a new instance of the PairSet class with a specified key comparer.

comparer
The equality comparer for keys, or null to use the default comparer.
PairSet
public PairSet()
pairSet = PairSet()

Initializes a new instance of the PairSet class with default comparers.

Properties

Item
public TKey Item { get; }
value = pairSet.Item

Gets the key associated with the specified value.

value
The value to look up.

Returns: The key associated with the specified value.

Methods

CanAdd
protected override bool CanAdd(TKey key, TValue value)
result = pairSet.CanAdd(key, value)

Determines whether a key-value pair can be added to the collection.

key
The key to check.
value
The value to check.

Returns: True if the value is not already associated with a key; otherwise, false.

ContainsValue
public bool ContainsValue(TValue value)
result = pairSet.ContainsValue(value)

Determines whether the collection contains a specific value.

value
The value to check.

Returns: True if the value exists in the collection; otherwise, false.

GetKey
public TKey GetKey(TValue value)
result = pairSet.GetKey(value)

Retrieves the key associated with the specified value.

value
The value to look up.

Returns: The key associated with the specified value.

GetValue
public TValue GetValue(TKey key)
result = pairSet.GetValue(key)

Retrieves the value associated with the specified key.

key
The key to look up.

Returns: The value associated with the specified key.

OnAdding
protected override void OnAdding(TKey key, TValue value)
pairSet.OnAdding(key, value)

Called before adding a key-value pair to the collection, updating the value-to-key mapping.

key
The key being added.
value
The value being added.
OnClearing
protected override void OnClearing()
pairSet.OnClearing()

Called before clearing the collection, clearing the value-to-key mapping.

OnRemoving
protected override void OnRemoving(TKey key, TValue value)
pairSet.OnRemoving(key, value)

Called before removing a key-value pair, updating the value-to-key mapping.

key
The key being removed.
value
The value being removed.
OnSetting
protected override void OnSetting(TKey key, TValue value)
pairSet.OnSetting(key, value)

Called before setting a new value for an existing key, updating the value-to-key mapping. Maintains bijective property by swapping values when a collision occurs.

key
The key being updated.
value
The new value to set.
RemoveByValue
public bool RemoveByValue(TValue value)
result = pairSet.RemoveByValue(value)

Removes a key-value pair from the collection based on the specified value.

value
The value to remove.

Returns: True if the value was found and removed; otherwise, false.

SetKey
public void SetKey(TValue value, TKey key)
pairSet.SetKey(value, key)

Sets a new key for an existing value, replacing any previous association.

value
The value to associate with the new key.
key
The new key to associate with the value.
SetValue
public void SetValue(TKey key, TValue value)
pairSet.SetValue(key, value)

Sets a new value for an existing key, updating the internal value-to-key mapping.

key
The key whose value is to be updated.
value
The new value to associate with the key.
TryAdd
public bool TryAdd(TKey key, TValue value)
result = pairSet.TryAdd(key, value)

Attempts to add a key-value pair to the collection if neither the key nor value already exists.

key
The key to add.
value
The value to add.

Returns: True if the pair was added; false if either the key or value already exists.

TryGetKey
public bool TryGetKey(TValue value, TKey key)
result = pairSet.TryGetKey(value, key)

Attempts to retrieve the key associated with a specified value.

value
The value to look up.
key
When this method returns, contains the key if found; otherwise, the default value of .

Returns: True if the value was found; otherwise, false.